import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from './http'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import moment from 'moment';
import { Message, Loading } from 'element-ui';

Vue.use(ElementUI)

Vue.config.productionTip = false;
Vue.prototype.$moment = moment
Vue.prototype.axios = axios
axios.interceptors.request.use(function(config) {
    // 这里的config包含每次请求的内容
    let token = sessionStorage.getItem('sessionId')
    if (token) {
        // 添加headers
        config.headers.Authorization = token;
        config.headers['content-type'] = 'application/json;charset=UTF-8';
    } else {}
    return config;
}, function(err) {
    return Promise.reject(err);
})


Vue.mixin({
    data() {
        return {
            root: "/online/pharmacyplatform",//线上
            // root: "/drugstore",
            // root:"",
            app: "yuanbao",
            // uploadUrl: "http:192.168.6.19:8000/imageUp/upload", //本地
            uploadUrl: "https://www.tencentyb.com/online/pharmacyplatform/imageUp/upload", //线上
            // uploadUrl: "http://192.168.6.242:8081/drugstore/imageUp/upload", //线上
            imgUpload:'/imageUp/upload',//本地
            // imgUpload:'/online/pharmacyplatform/imageUp/upload',//线上
            // imgUpload:'/drugstore/imageUp/upload',//线上
            loadingAnimate: '',
            websocketUrl: "wss://www.tencentyb.com/online/pharmacyplatform/websocket/",//线上
            // websocketUrl: "ws://192.168.6.19:8000/websocket/"
        }
    },
    methods: {
        post(url, para, success, isAnimate = true) {
            if (isAnimate) this.startLoading()
            axios.post(this.root + url, para).then((res) => {
                if (isAnimate) this.endLoading()
                if (res.data.success || res.data.code == '200') {
                    success(res.data)
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }, (err) => {
                if (isAnimate) this.endLoading()
            })
        },
        get(url, para, success, isAnimate = true) {
            if (isAnimate) this.startLoading()
            axios.get(this.root + url, { para }).then((res) => {
                if (isAnimate) this.endLoading()
                if (res.data.success || res.data.code == '200') {
                    success(res.data)
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }, (err) => {
                if (isAnimate) this.endLoading()
            })
        },
        getUploadUrl() {
            return this.uploadUrl
        },
        // 深度克隆数据
        deepClone(obj, newObj = {}) {
            for (let key in obj) {
                if (typeof obj[key] == 'object') {
                    newObj[key] = (obj[key].constructor === Array) ? [] : {}
                    this.deepClone(obj[key], newObj[key]);
                } else {
                    newObj[key] = obj[key]
                }
            }
            return newObj;
        },
        // 身份证验证
        verifyIdcard(value) {
            let reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
            if (reg.test(value)) {
                return true
            } else {
                return false
            }
        },
        // 手机号验证
        verifyPhone(value) {
            let regular = /^1[3|4|5|7|8]\d{9}$/;
            if (regular.test(value)) {
                return true
            } else {
                return false
            }
        },
        // 开始动画
        startLoading() {
            this.loadingAnimate = Loading.service({
                lock: true,
                text: '拼命加载中...',
                background: 'rgba(0,0,0,0.7)'
            });
        },
        // 结束动画
        endLoading() {
            this.loadingAnimate.close()
        },
        backtop() {
            let timer = setInterval(function() {
                let osTop = document.documentElement.scrollTop || document.body.scrollTop;
                let ispeed = Math.floor(-osTop / 5);
                document.documentElement.scrollTop = document.body.scrollTop = osTop + ispeed;
                this.isTop = true;
                if (osTop === 0) {
                    clearInterval(timer);
                }
            }, 30)
        }
    },
})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')